import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/common/choice_bean.dart';

class CustomField extends StatelessWidget {
  const CustomField({
    required this.title,
    this.right,
    super.key,
    this.isRequired = false,
    this.bottom,
    this.titleStyle = const TextStyle(
      fontSize: 14,
      height: 1.2,
      color: Color(0xFF262F38),
      fontWeight: FontWeight.w400,
    ),
  });

  final String title;
  final TextStyle titleStyle;
  final Widget? right;
  final bool isRequired;
  final Widget? bottom;

  @override
  Widget build(BuildContext context) {
    final top = Row(
      children: [
        if (isRequired) const Text('*', style: TextStyle(color: Colors.red)),
        Text(
          title,
          style: titleStyle,
        ),
        AppGaps.hGap10,
        Expanded(
          child: Align(
            alignment: Alignment.centerRight,
            child: right,
          ),
        ),
      ],
    );
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 7.w),
      height: bottom == null ? 44.w : null,
      child: bottom == null
          ? top
          : Column(
              children: [
                top,
                bottom!,
              ],
            ),
    );
  }

  static Widget select({
    required List<String> options,
    required void Function(String) callback,
    String? selected,
  }) {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        const Expanded(child: SizedBox()),
        Text(
          selected ?? '请选择',
          style: TextStyle(
            fontSize: 14.sp, height: 1.2, color: selected == null ? const Color(0xFFA0A7B5) : const Color(0xFF666666), //const Color(0xFF262F38),
          ),
          textAlign: TextAlign.end,
        ),
        AppGaps.hGap6,
        Icon(Icons.arrow_forward_ios, size: 14.sp, color: AppColors.text_grey)
      ],
    ).clickOnce(() {
      final beans = options.map((e) => SimpleChoiceBean(e, e)).toList();
      CustomSelectFunction.selectMultipleBottomSheet<ChoiceBean>(
        onCallBack: (val) => callback(val.first.getShowName()),
        dataList: beans,
        checkedList: selected == null ? [] : [SimpleChoiceBean(selected, selected)],
        isMultiple: false,
      );
    });
  }

  /// [hide]: 是否仅在编辑时显示
  static Widget input({
    String hint = '请输入',
    TextEditingController? controller,
    bool isRequired = false,
    bool hide = false,
    void Function(String)? onChanged,
  }) {
    var obscure = hide;
    return SizedBox(
      height: 31.w,
      child: StatefulBuilder(builder: (_, setState) {
        return TextField(
          controller: controller,
          decoration: InputDecoration(
            hintText: hint,
            hintStyle: TextStyle(
              fontSize: 14.sp, height: 1.2, color: const Color(0xFFA0A7B5), //const Color(0xFF262F38),
            ),
            border: InputBorder.none,
          ),
          obscureText: obscure,
          onTapOutside: (_) {
            if (hide) setState(() => obscure = true);
          },
          textAlign: TextAlign.end,
          onChanged: onChanged,
        ).clickOnce(() {
          if (hide) setState(() => obscure = false);
        });
      }),
    );
  }
}
